package com.purplesoft.zettl.Provider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

import com.purplesoft.zettl.Database.DatabaseManager;
import com.purplesoft.zettl.DatabaseTables.TblCart;
import com.purplesoft.zettl.DatabaseTables.TblCategories;
import com.purplesoft.zettl.DatabaseTables.TblItems;
import com.purplesoft.zettl.DatabaseTables.TblLists;
import com.purplesoft.zettl.DatabaseTables.TblManufacturer;
import com.purplesoft.zettl.DatabaseTables.TblMeasures;
import com.purplesoft.zettl.DatabaseTables.TblPurchase;
import com.purplesoft.zettl.DatabaseViews.VwCartItems;
import com.purplesoft.zettl.DatabaseViews.VwItems;
import com.purplesoft.zettl.DatabaseViews.VwPurchaseItems;

public class DatabaseContentProvider extends ContentProvider {

	private static final String AUTHORITY = "com.purplesoft.zettl";
	public static final Uri VWPurchaseItems = Uri.parse("content://"
			+ AUTHORITY + "/" + VwPurchaseItems.viewName);
	public static final Uri VWCartItems = Uri.parse("content://"
			+ AUTHORITY + "/" + VwCartItems.viewName);
	public static final Uri VWItems = Uri.parse("content://"
			+ AUTHORITY + "/" + VwItems.viewName);
	public static final Uri TBLPurchase = Uri.parse("content://"
			+ AUTHORITY + "/" + TblPurchase.tableName);
	public static final Uri TBLItems = Uri.parse("content://"
			+ AUTHORITY + "/" + TblItems.tableName);
	public static final Uri TBLCategories = Uri.parse("content://"
			+ AUTHORITY + "/" + TblCategories.tableName);
	public static final Uri TBLManufacturer = Uri.parse("content://"
			+ AUTHORITY + "/" + TblManufacturer.tableName);
	public static final Uri TBLMeasures = Uri.parse("content://"
			+ AUTHORITY + "/" + TblMeasures.tableName);
	public static final Uri TBLCart = Uri.parse("content://"
			+ AUTHORITY + "/" + TblCart.tableName);
	public static final Uri TBLLists = Uri.parse("content://"
			+ AUTHORITY + "/" + TblLists.tableName);
	
	private DatabaseManager dbManager;
	private static final UriMatcher uriMatcher;
	private static final int vwPurchaseItems = 1;
	private static final int vwCartItems = 2;
	private static final int vwItems = 3;
	private static final int tblPurchase = 4;
	private static final int tblItems = 5;
	private static final int tblCategories = 6;
	private static final int tblManufacturers = 7;
	private static final int tblMeasures = 8;
	private static final int tblCart = 9;
	private static final int tblLists =10;

	private static String TAG = DatabaseContentProvider.class.getName();

	static {
		uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
		uriMatcher.addURI(AUTHORITY, VwPurchaseItems.viewName, vwPurchaseItems);
		uriMatcher.addURI(AUTHORITY, VwCartItems.viewName, vwCartItems);
		uriMatcher.addURI(AUTHORITY, VwItems.viewName, vwItems);
		uriMatcher.addURI(AUTHORITY, TblPurchase.tableName, tblPurchase);
		uriMatcher.addURI(AUTHORITY, TblItems.tableName, tblItems);
		uriMatcher.addURI(AUTHORITY, TblCategories.tableName, tblCategories);
		uriMatcher.addURI(AUTHORITY, TblManufacturer.tableName, tblManufacturers);
		uriMatcher.addURI(AUTHORITY, TblMeasures.tableName, tblMeasures);
		uriMatcher.addURI(AUTHORITY, TblCart.tableName, tblCart);
		uriMatcher.addURI(AUTHORITY, TblLists.tableName, tblLists);
	}

	public DatabaseContentProvider() {
	}

	@Override
	public boolean onCreate() {
		try {
			dbManager = new DatabaseManager(getContext());
			return true;
		} catch (Exception e) {
			Log.d(TAG, "onCreate() " + e);
			return true;
		}
	}

	@Override
	public String getType(Uri uri) {
		switch (uriMatcher.match(uri)) {
		case vwPurchaseItems:
			return VwPurchaseItems.CONTENT_TYPE;
		case vwItems:
			return VwItems.CONTENT_TYPE;

		}
		return null;
	}

	@Override
	public Cursor query(Uri uri, String[] columns, String whereClause,
			String[] whereClauseArgs, String orderBy) {
		try {
			int result = uriMatcher.match(uri);
			switch (result) {
			case vwPurchaseItems:
				return dbManager.selectVwPurchaseItems(columns, whereClause, whereClauseArgs, orderBy);
			case vwCartItems:
				return dbManager.selectVwCartItems(columns, whereClause, whereClauseArgs, orderBy);
			case vwItems:
				return dbManager.selectVwItems(columns, whereClause, whereClauseArgs, orderBy);
			case tblPurchase:
				return dbManager.selectTblPurchase(columns, whereClause, whereClauseArgs, orderBy);
			case tblItems:
				return dbManager.selectTblItems(columns, whereClause, whereClauseArgs, orderBy);
			case tblCategories:
				return dbManager.selectTblCategories(columns, whereClause, whereClauseArgs, orderBy);
			case tblManufacturers:
				return dbManager.selectTblManufacturers(columns, whereClause, whereClauseArgs, orderBy);
			case tblMeasures:
				return dbManager.selectTblMeasures(columns, whereClause, whereClauseArgs, orderBy);
			case tblCart:
				return dbManager.selectTblCart(columns, whereClause, whereClauseArgs, orderBy);
			case tblLists:
				return dbManager.selectTblLists(columns, whereClause, whereClauseArgs, orderBy);
			default:
				return null;
			}
		} catch (Exception e) {
			Log.d(TAG, "Cursor query " + e);
			return null;
		}
	}

	@Override
	public Uri insert(Uri uri, ContentValues contentValues) {
		try {
			int result = uriMatcher.match(uri);
			switch (result) {
			case tblPurchase:
				dbManager.insertTblPurchase(contentValues);
				break;
			case tblItems:
				dbManager.insertTblItmes(contentValues);
				break;
			case tblCategories:
				dbManager.insertTblCategories(contentValues);
				break;
			case tblManufacturers:
				dbManager.insertTblManufacturers(contentValues);
				break;
			case tblMeasures:
				dbManager.insertTblMeasures(contentValues);
				break;
			case tblCart:
				dbManager.insertTblCart(contentValues);
				break;
			case tblLists:
				dbManager.insertTblLists(contentValues);
				break;
			default:
				return null;
			}
		} catch (Exception e) {
			Log.d(TAG, "Cursor query " + e);
			return null;
		}
		return null;
	}

	@Override
	public int update(Uri uri, ContentValues contentValues, String whereClause,
			String[] whereClauseArgs) {
		int result = uriMatcher.match(uri);
		switch (result) {
		case tblPurchase:
			dbManager.updateTblPurchase(contentValues, whereClause, whereClauseArgs);
			break;
		case tblItems:
			dbManager.updateTblItems(contentValues, whereClause, whereClauseArgs);
			break;
		case tblCategories:
			dbManager.updateTblCategories(contentValues, whereClause, whereClauseArgs);
			break;
		case tblManufacturers:
			dbManager.updateTblManufacturers(contentValues, whereClause, whereClauseArgs);
			break;
		case tblMeasures:
			dbManager.updateTblMeasures(contentValues, whereClause, whereClauseArgs);
			break;
		case tblCart:
			dbManager.updateTblCart(contentValues, whereClause, whereClauseArgs);
			break;
		case tblLists:
			dbManager.updateTblLists(contentValues, whereClause, whereClauseArgs);
			break;
		default:
			return 0;
		}
		return 0;
	}

	@Override
	public int delete(Uri uri, String whereClause, String[] whereClauseArgs) {
		int result = uriMatcher.match(uri);
		switch (result) {
		case tblPurchase:
			dbManager.deleteTblPurchase(whereClause, whereClauseArgs);
			break;
		case tblItems:
			dbManager.deleteTblItems(whereClause, whereClauseArgs);
			break;
		case tblCategories:
			dbManager.deleteTblCategories(whereClause, whereClauseArgs);
			break;
		case tblManufacturers:
			dbManager.deleteTblManufacturers(whereClause, whereClauseArgs);
			break;
		case tblMeasures:
			dbManager.deleteTblMeasures(whereClause, whereClauseArgs);
			break;
		case tblCart:
			dbManager.deleteTblCart(whereClause, whereClauseArgs);
			break;
		case tblLists:
			dbManager.deleteTblLists(whereClause, whereClauseArgs);
			break;
		default:
			return 0;
		}
		return 0;
	}

}
